package oa.struts.action;

import java.io.File;
import java.util.Collection;
import java.util.ListIterator;
import java.util.Set;
import java.util.TreeSet;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import oa.annotation.privilege.PrivilegeInfo;
import oa.domain.page.PageBean;
import oa.domain.system.Department;
import oa.domain.system.Role;
import oa.domain.system.User;
import oa.service.DepartmentService;
import oa.service.RoleService;
import oa.service.UserService;
import oa.struts.action.base.BaseAction;
import oa.util.HQLHelper;
import oa.util.OAUtils;
import oa.util.UploadUtils;

@Controller("userAction")
public class UserAction extends BaseAction<User>{

	@Resource(name="userService")
	private UserService userService;
	
	@Resource(name="departmentService")
	private DepartmentService departmentService;
	
	@Resource(name="roleService")
	private RoleService roleService;
	
    private Long did;
    private Long[] rids;
    
	public Long getDid() {
		return did;
	}


	public void setDid(Long did) {
		this.did = did;
	}


	public Long[] getRids() {
		return rids;
	}


	public void setRids(Long[] rids) {
		this.rids = rids;
	}


	@PrivilegeInfo(name="用户查询")
	public String showAllUsers()
	{
//		Collection<User> userList = this.userService.getAllUsers();
//		ActionContext.getContext().put("userList", userList);
		HQLHelper hh = new HQLHelper(User.class);
		
		PageBean pageBean = this.userService.getPageBean(hh,currentPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return listAction;
	}
	
	public String addUI()
	{
		/**
		 * 把部门的信息和岗位的信息全部提取出来
		 *    加入两个service
		 */
		Collection<Department> dList = this.departmentService.getAllDeaprtment();
		Collection<Role> rList = this.roleService.getAllRole();
		ActionContext.getContext().put("dList", dList);
		ActionContext.getContext().put("rList", rList);
		return addUI;
	}
	
	public String add()
	{
		/**
		 * 1、获取用户的一般属性
		 *    利用模型驱动可以获取
		 * 2、获取页面上选中的部门ID的值
		 * 3、获取岗位的IDS的值(该值有可能有多个)
		 * 4、在保存用户的时候，建立用户和部门之间的关系
		 * 5、在保存用户的时候，建立用户和岗位之间的关系
		 */
		User user = new User();
		BeanUtils.copyProperties(this.getModel(), user);
		this.userService.saveUser(did, rids, user);		 
		return action2action;
	}
	
	public String updateUI()
	{
		/**
		 * 1、涉及到用户一般属性的回显
		 * 2、部门信息的回显
		 * 3、岗位信息的回显
		 * 4、把部门信息和岗位信息全部提取出来
		 */
		User user = this.userService.getUserById(this.getModel().getUid());
		ActionContext.getContext().getValueStack().push(user);
		
		this.did = user.getDepartment().getDid();
		Set<Role> roles = user.getRoles();
		
		this.rids = new Long[roles.size()];
		int i = 0 ;
		for(Role role: roles)
		{
			rids[i] = role.getRid();
			i++;
		}
		
        Collection<Department> dList = this.departmentService.getAllDeaprtment();
        Collection<Role> rList = this.roleService.getAllRole();
        ActionContext.getContext().put("dList", dList);
        ActionContext.getContext().put("rList", rList);
		return updateUI;
	}
	
	public String update()
	{
		User user = new User();
		BeanUtils.copyProperties(this.getModel(), user);
		userService.updateUser(did, rids, user);
		return action2action;
	}
	
	public String delete()
	{
		this.userService.deleteUser(this.getModel().getUid());
		return action2action;
	}
	
	public String intiPassword()
	{
		User user = this.userService.getUserById(this.getModel().getUid());
		user.setPassword("123456");
	    this.userService.updateUser(user);
		return action2action;
	}

}
